package com.miyake.loveviewweb.client.plugin;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.miyake.loveviewweb.client.commonui.ComboBoxEx;
import com.miyake.loveviewweb.client.dialog.DialogHandler;
import com.miyake.loveviewweb.client.dialog.IDialogPanel;
import com.miyake.loveviewweb.client.service.DataService;
import com.miyake.loveviewweb.shared.model.DataModel;
import com.miyake.loveviewweb.shared.model.PluginModel;
import com.miyake.loveviewweb.shared.model.ValueModel;

public class InstalledPluginListCombo extends Composite implements IDialogPanel {
    private ComboBoxEx<PluginModel> listBox;
    private PluginSettingPanel pluginSettingPanel;
    
    public InstalledPluginListCombo(final String type, final DataModel dataModel) {
        VerticalPanel panel = new VerticalPanel();
        initWidget(panel);

        HorizontalPanel sub = new HorizontalPanel();
        panel.add(sub);
        listBox = new ComboBoxEx<PluginModel>();
        sub.add(listBox);
        Anchor link = new Anchor("Search plugins");
        link.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                Window.open(GWT.getHostPageBaseURL()+"?startup=pluginMarket", "_blank", "");
            }  
        });
        sub.add(link);
        final SimplePanel setupPanel = new SimplePanel();
        panel.add(setupPanel);        
        getInstalledPlugins(type, setupPanel, dataModel);
        
        listBox.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                getInstalledPlugins(type, setupPanel, dataModel);
            }
        });
        
        listBox.addChangeHandler(new ChangeHandler() {

            public void onChange(ChangeEvent event) {
                updateSettingPanel(setupPanel, dataModel);
            }
            
        });
        
    }

    private void getInstalledPlugins(String type, final SimplePanel setupPanel, final DataModel dataModel) {
        DataService.Util.getInstance().getInstalledPlugins(type, new AsyncCallback<List<PluginModel>>() {
            public void onFailure(Throwable caught) { 
                Window.alert(caught.getMessage());
            }

            public void onSuccess(List<PluginModel> result) {
                if (isChanged(result)) {
                    listBox.clear();
                    for (PluginModel pluginModel : result ){
                        listBox.addItem(pluginModel.getServiceName(), pluginModel);
                    }
                    updateSettingPanel(setupPanel, dataModel);
                }
                
                
            }
        });
    }
    
    protected boolean isChanged(List<PluginModel> result) {
        for (PluginModel plugin : result) {
            boolean found = false;
            for (int i = 0; i < listBox.getItemCount(); i++) {
                if (listBox.getItemText(i).equals(plugin.getServiceName())) {
                    found = true;
                }
            }
            if (!found) {
                return true;
            }
        }
        return false;
    }

    /* (non-Javadoc)
     * @see com.miyake.loveviewweb.client.dialog.IPluginList#getSelectedItem()
     */
    public PluginModel getSelectedItem() {
        return listBox.getSelectedObject();
    }

    private void updateSettingPanel(final SimplePanel setupPanel, DataModel dataModel) {
        setupPanel.clear();
        this.pluginSettingPanel = new PluginSettingPanel(listBox.getSelectedObject(), dataModel);
        setupPanel.add(pluginSettingPanel);
    }
    
    /* (non-Javadoc)
     * @see com.miyake.loveviewweb.client.dialog.IPluginList#getValues()
     */
    public List<ValueModel> getValues() {
        return this.pluginSettingPanel.getValues();
    }

    public Map<String, String> getData() {
        return this.pluginSettingPanel.getData();
    }
    
    public Composite getUi() {
        return this;
    }

    public void setDialogHandler(DialogHandler handler) {
        // TODO Auto-generated method stub
        
    }

    public Composite getComposite() {
        return this;
    }

}
